package com.xue.web.dao;

import com.xue.web.utils.JdbcTools;
import com.xue.web.utils.SnowFlakeUtil;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


public class RegisterDao {
   private final JdbcTools jdbcTools =new JdbcTools("jdbc.properties");

    /**
     *
      * @param userName 用户名
     * @param userPassword 用户密码
     * @param userEmail 用户邮箱
     * @return 成功/失败
     */
   public  boolean insertUser(String userName,String userPassword,String userEmail ){
        System.out.println(this.getClass().getSimpleName()+"|执行插入中");
        try {
            jdbcTools.executeInsert(
                    "INSERT INTO x_user (user_id, user_name, user_password, user_email) VALUES (?, ?, ?, ?); ",
                    new Object[]{SnowFlakeUtil.getSnowFlakeId(),userName,userPassword,userEmail});
        } catch (SQLException | IOException | ClassNotFoundException e) {
            System.out.println(this.getClass().getSimpleName()+"|发生"+e+"异常");
            return false;
        }
        return true;
    }

    /**查询用户名是否已经存在
     *
     * @param userName 用户名称
     * @return 查到的用户唯一识别码[一组]
     */
    public Map<String, List<Object>> selectUser(String userName){
        System.out.println(this.getClass().getSimpleName()+"|执行查询");
        try {
            return jdbcTools.executeQuery("SELECT user_id FROM x_user where user_name = ?", new Object[]{userName});
        } catch (SQLException | ClassNotFoundException | IOException e) {
            System.out.println(this.getClass().getSimpleName()+"|查询时发生"+e+"异常");
            return null;
        }
    }

}
